
import 'package:flutter/cupertino.dart';
import 'package:gczn_hd/app/data/local/preference/preference_manager.dart';
import 'package:gczn_hd/app/data/repository/gczn_repository.dart';
import 'package:gczn_hd/app/modules/login/model/user_response.dart';
import 'package:gczn_hd/flavors/build_config.dart';
import 'package:gczn_hd/flavors/env_config.dart';

import '/app/core/base/base_controller.dart';
import 'package:get/get.dart';

class HomeController extends BaseController {
  late GcznRepository _gcRequest;
  late PreferenceManager _preferenceManager;
  final EnvConfig config = BuildConfig.instance.config;


  final TextEditingController passwordController = TextEditingController();
  final Rx<DateTime> lastPressedAt = DateTime.now().obs;
  /// 用户数据
  final Rx<UserResponse> userData = Rx<UserResponse>(UserResponse());

  onViewInit() async {
    lastPressedAt.value = DateTime.now();
    _gcRequest = Get.find(tag: (GcznRepository).toString());
    _preferenceManager = Get.find(tag: (PreferenceManager).toString());


    userData.value  = await _preferenceManager.getStringTtoObject<UserResponse>(PreferenceManager.keyUserData, UserResponse.fromJson) ?? UserResponse();


  }

  Future<void> logout() async {
    bool isLogout = await callDataService(_gcRequest.logout());
    if( isLogout ){
      config.token = "";
      _preferenceManager.clear();
      Get.offAllNamed("/login");
    }
  }

  @override
  void onClose() {
    passwordController.dispose();
    super.onClose();
  }

}
